{% extends "layout.html" %}
{% block content %}
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">表单</div>
            <div class="panel-body">
                <form id="formAdd">
                    {% for field in form %}
                        <div class="col-xs-6">
                            <div class="form-group" style="position:relative; margin-bottom:20px">
                                <label>{{ field.label }}</label>
                                {{ field }}
                                <span class="error_msg" style="color:darkred;position:absolute"></span>
                            </div>
                        </div>
                    {% endfor %}
                    <div class="col-xs-12">
                        <input id="btnAdd" type="button" class="btn btn-default" value="提交"/>
                    </div>
                </form>
            </div>
        </div>
        <div>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
                    任务列表
                </div>
                <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>级别</th>
                        <th>标题</th>
                        <th>负责人</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for task in queryset %}
                        <tr>
                            <th>{{ task.id }}</th>
                            <td>{{ task.get_level_display }}</td>
                            <td>{{ task.title }}</td>
                            <td>{{ task.user.username }}</td>
                            <td><a class="btn btn-primary btn-xs" href="/task/{{ task.id }}/edit/">编辑</a>
                                <a class="btn btn-danger btn-xs" href="/task/{{ task.id }}/delete/">删除</a>
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        {{ page_html }}
    </div>
{% endblock %}
{% block js %}
    <script type="text/javascript">
        $(function () {
            bindBtnAddEvent();
        });

        function bindBtnAddEvent() {
            $("#btnAdd").click(function () {
                $(".error_msg").empty();
                $.ajax({
                        url: "/task/add/",
                        type: "post",
                        data: $("#formAdd").serialize(),
                        success: function (res) {
                            {#解析JSON字符串#}
                            parsedRes = $.parseJSON(res)
                            if (parsedRes['status']) {
                                alert('数据添加成功！');
                                location.reload();
                            } else {
                                errorObj = $.parseJSON(res)['errors'];
                                $.each(errorObj, function (name, data) {
                                    $("#id_" + name).next().text(data[0]);
                                });
                            }
                        }
                    }
                )
            })
        }
    </script>
{% endblock %}